/**
* 处理网络IO的子进程
* @author thinkry
*/

#ifndef __MPSVR_NET_H__
#define __MPSVR_NET_H__

#include "mpdef.h"
#include "net/CTEpoll.h"
#include "CTShmQueue.h"
#include "mpnetclientfactory.h"

/**
* 处理网络IO的子进程
*/

#define CMpNetEpollObject CTEpollObject<CMpNet>
#define CMpNetShmQueue CTShmQueue<CMpNet>

class CMpNet
{
    friend class CMpNetClient;
protected:
    static bool s_bReloadConfig;
    static bool s_bExit;

    CTStat				m_stStat;                   ///< 统计对象
    CTEpoll             m_stEpoll;                  ///< epoll对象

    CTShmQueue<CMpNet>      m_stShmQueue;

    CTSocket                m_stServerSocket;           ///< 与工作进程通讯的Socket
    CTEpollObject<CMpNet>   m_stServerObject;

    char*                 m_pszClientMemory;
    CMpNetClientFactory   m_stClientFactory;

    enum
    {
        EPOLL_SIZE = 1024,
        EPOLL_WAIT_SIZE = 1024,
    };

public:
    CMpNet();
    virtual ~CMpNet();

    /** 初始化 */
    void Init();

    /** 主循环 */
    void Run();

    /** 退出 */
    void Exit();

    static void sigusr1_handle(int iSigVal);
    static void sigusr2_handle(int iSigVal);

    void OnServerEvent(CMpNetEpollObject* pstObject, SOCKET iSocket, int iEvent);
    void OnClientEvent(CMpNetEpollObject* pstObject, SOCKET iSocket, int iEvent);
    void OnShmQueueEvent(CMpNetEpollObject* pstObject, SOCKET iSocket, int iEvent);

protected:
    void ReloadConfig();
    void SendRemainData();

    void HandleShmQueueMsg();

    void NotifyClientClose(CMpNetClient* pstClient);
    void OnClientClose(CMpNetClient* pstClient);

    int GetMaxRunCount();

    void DumpStatInfo();
};

#endif //__MPSVR_NET_H__
